//
//  BaseTableViewCell.swift
//  app
//
//  Created by gy on 2023/9/6.
//

import UIKit

class BaseTableViewCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        separatorInset = UIEdgeInsets(top: 0, left: screenWidth*2, bottom: 0, right: 0)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class BaseCornerTableViewCell: UITableViewCell {

    /// 背景颜色
    public var cornerBackColor: UIColor = UIColor.white {
        didSet {
            noneCornerView.backgroundColor = cornerBackColor
            allCornerView.backgroundColor = cornerBackColor
            topCornerView.backgroundColor = cornerBackColor
            bottomCornerView.backgroundColor = cornerBackColor
        }
    }
    /// 左右间距
    public var backHorizontalMargin: CGFloat = 16 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.left.equalTo(backHorizontalMargin)
                make.right.equalTo(-backHorizontalMargin)
            }
        }
    }
    /// 上下间距
    public var backVerticalMargin: CGFloat = 0 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.top.equalTo(backVerticalMargin)
                make.bottom.equalTo(-backVerticalMargin)
            }
        }
    }
    /// 上间距
    public var backTopMargin: CGFloat = 0 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.top.equalTo(backTopMargin)
            }
        }
    }
    /// 下间距
    public var backBottomMargin: CGFloat = 0 {
        didSet {
            noneCornerView.snp.updateConstraints { make in
                make.bottom.equalTo(-backBottomMargin)
            }
        }
    }
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        subLayout()
        
        contentView.layoutIfNeeded()
        // 设置圆角
        DispatchQueue.main.async {
            self.topCornerView.layerCornerRadius(8, [.topLeft,.topRight])
            self.bottomCornerView.layerCornerRadius(8, [.bottomLeft,.bottomRight])
            self.allCornerView.layerCornerRadius(8, [.allCorners])
        }
    }
    
    // 设置cell圆角
    public func setCorner(direction: AppCellRectCorner) {
        
        topCornerView.isHidden = !(direction == .top)
        bottomCornerView.isHidden = !(direction == .bottom)
        allCornerView.isHidden = !(direction == .all)
        noneCornerView.isHidden = !(direction == .none)
        
    }
    
    // MARK: - configUI
    func subLayout() {
        
        selectionStyle = .none
        backgroundColor = .clear
        separatorInset = UIEdgeInsets(top: 0, left: screenWidth*2, bottom: 0, right: 0)
        
        noneCornerView.backgroundColor = cornerBackColor
        allCornerView.backgroundColor = cornerBackColor
        topCornerView.backgroundColor = cornerBackColor
        bottomCornerView.backgroundColor = cornerBackColor
        
        contentView.addSubview(noneCornerView)
        contentView.addSubview(allCornerView)
        contentView.addSubview(topCornerView)
        contentView.addSubview(bottomCornerView)

        noneCornerView.snp.makeConstraints { make in
            make.left.equalTo(backHorizontalMargin)
            make.right.equalTo(-backHorizontalMargin)
            make.top.equalTo(backVerticalMargin)
            make.bottom.equalTo(-backVerticalMargin)
        }
        allCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        topCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        bottomCornerView.snp.makeConstraints { make in
            make.edges.equalTo(noneCornerView)
        }
        
    }
    private(set) lazy var noneCornerView = UIView()
    private(set) lazy var allCornerView = UIView()
    private(set) lazy var topCornerView = UIView()
    private(set) lazy var bottomCornerView = UIView()
    
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    open override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class BaseEmptyTableViewCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    
    static func getCellHeight() -> CGFloat {
        return 140 + 60
    }
    func setInfo(iconName: String, title: String) {
        if iconName.contains("http") {
            imgView.sd_setImage(with: URL(string: iconName), placeholderImage: nil)
        } else {
            imgView.image = UIImage(named: iconName)
        }
        promptLabel.text = title
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.addSubview(backView)
        backView.addSubview(imgView)
        backView.addSubview(promptLabel)
        
        backView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        imgView.snp.makeConstraints { make in
            make.height.equalTo(60)
            make.top.centerX.equalToSuperview()
        }
        promptLabel.snp.makeConstraints { make in
            make.top.equalTo(imgView.snp.bottom).offset(10)
            make.left.right.bottom.centerX.equalToSuperview()
        }
        
        imgView.contentMode = .scaleAspectFit
    }
    public lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    public lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    public lazy var promptLabel: UILabel = {
        let promptLabel = UILabel()
        promptLabel.font = kSetPingFangRegular(14)
        promptLabel.textColor = UIColor(hex: 0xAEAEAE)
        promptLabel.textAlignment = .center
        return promptLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}



